/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hibernate.dao;

import hibernate.entity.Login;
import hibernate.entity.MesageConversation;
import hibernate.entity.Message;

/**
 *
 * @author NDH
 */
public class MesConDAO extends AbstractDAO{
     public int create(MesageConversation mc) {
        try {
            tx = session.beginTransaction();
            session.save(mc);
            tx.commit();
            return 1;
        } catch (Exception e) {
            return -1;
        }
    }
    
     public MesageConversation getLastOfConver(int id){
         query  = session.createQuery("from MessageConversations as mc join mc.Conversations"
                 + " as c with c.converId = :id");
         try {
            return (MesageConversation) query.list().get(0);
         } catch (Exception e) {
             return null;
         }
         
     }
     public MesageConversation readBy(Login receiver, Message mes){
         query  = session.createQuery("from MessageConversation mc where mc.receiver = :receiver "
                 + " and mc.messages = :messages");
         query.setParameter("messages", mes);
         query.setParameter("receiver", receiver);
         try {
            return (MesageConversation) query.list().get(0);
         } catch (Exception e) {
             return null;
         }
         
     }
     
     
      public static void main(String[] args) {
        new MesConDAO().getLastOfConver(1);
    }
}
